TOOL.Category = "NPC Control"
TOOL.Name = "NPC Attack Commander"
TOOL.Command = nil
TOOL.ConfigName = ""

if CLIENT then
  language.Add( "Tool_npc_relations_name", "NPC Attacker" )
  language.Add( "Tool_npc_relations_desc", "Select and tell multiple NPCs to attack something" )
  language.Add( "Tool_npc_relations_0", "Left click to select an NPC, Right-click to tell them to attack; Reload to disband all" )
end

TOOL.ClientConVar["attackback"] = "1"
TOOL.ClientConVar["reloadboth"] = "1"
TOOL.ClientConVar["undo"] = "0"

local SpeakOnSelect = {}
SpeakOnSelect.npc_combine_s = "selected_combine.wav"
SpeakOnSelect.npc_metropolice = "selected_cop.wav"
SpeakOnSelect.npc_citizen = "selected_citizen.wav"

local function npc_doNotify( message, symbol, user )
  if SERVER then
    user:SendLua("GAMEMODE:AddNotify(\"" .. message .. "\", " .. symbol .. ", \"5\"); surface.PlaySound( \"ambient/water/drip" .. math.random(1, 4) .. ".wav\" )")
  end
end

selectedNpcs_relations = {}

function TOOL:LeftClick( trace )
  //Ripped right from the mover gun :D
  if (trace.Hit && !trace.HitWorld) then
    if (trace.Entity:IsNPC()) then
      if (selectedNpcs_relations[trace.Entity] == nil) then
        //Select
        if (SpeakOnSelect[trace.Entity:GetClass()] != nil) then
          local ToSpeak = Sound( SpeakOnSelect[trace.Entity:GetClass()] )
          trace.Entity:EmitSound(ToSpeak)
        end
        selectedNpcs_relations[trace.Entity] = trace.Entity
        npc_doNotify("NPC Selected", "NOTIFY_GENERIC", self:GetOwner())
      else
        //Un-select
        selectedNpcs_relations[trace.Entity] = nil
        npc_doNotify("NPC un-selected", "NOTIFY_GENERIC", self:GetOwner())
      end
    end
    return true
  end
  return false
end

function TOOL:RightClick( trace )
  local runto = trace.HitPos
  //They can attack players :)
  if (GetConVarNumber("npc_control_allow_attacking") == 1 || SinglePlayer()) then
    if (trace.Entity:IsValid()) then
      for i, v in pairs(selectedNpcs_relations) do
        //Make sure it hasn't been un-selected
        local NPC = v
        if (NPC:IsValid()) then
          if (SERVER) then
            local doUndo = self:GetClientNumber("undo", 0)
            if (doUndo == 0) then
              NPC:AddEntityRelationship(trace.Entity, 1, 100)
              NPC:SetTarget(trace.Entity)
            else
              NPC:AddEntityRelationship(trace.Entity, 4, 100)
            end
            if (self:GetClientNumber("attackback") == 1) then
              if (doUndo == 0) then
                trace.Entity:AddEntityRelationship(NPC, 1, 100)
              else
                trace.Entity:AddEntityRelationship(NPC, 4, 100)
              end
            end
          end
        end 
      end
    end
  else
    npc_doNotify("This function is disabled on this server", "NOTIFY_ERROR", self:GetOwner())
  end
  return true
end

function TOOL:Reload( trace )
  selectedNpcs_relations = {}
  npc_doNotify("All NPCs un-selected", "NOTIFY_ERROR", self:GetOwner())
end

function TOOL.BuildCPanel(panel)
  panel:AddControl("Header", { Text = "NPC Commander", Description = "Used to make multiple NPCs attack at once" })
  panel:AddControl("CheckBox", { Label = "Make attackee's attack back", Command = "npc_relations_attackback"})
  panel:AddControl("CheckBox", { Label = "Reverse affects", Command = "npc_relations_undo"})
end

